package com.slipper.system.message.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.core.message.MessageUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.message.dao.MessageDao;
import com.slipper.system.message.bo.MessageBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class MessageService {
    @Resource
    private MessageDao messageDao;
    @Resource
    private MessageStatusService messageStatusService;

    public MessageBO queryBy(String id) {
        return messageDao.queryBy(id);
    }
    
    public List<MessageBO> queryList(MessageBO bo) {
        return messageDao.queryList(bo);
    }
    
    public PageInfo<MessageBO> queryPage(MessageBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(messageDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(MessageBO bo) {
        if (MessageBO.TYPE_NOTICE.equals(bo.getType())) {
            bo.setReceiveType(MessageBO.RECEIVE_TYPE_ALL);
            bo.setReceiverIds(null);
        } else if (MessageBO.TYPE_MESSAGE.equals(bo.getType())) {
            bo.setSystemIds(null);
        }

        int result;
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            messageStatusService.save(bo.getId(), MessageBO.RECEIVE_TYPE_ALL.equals(bo.getReceiveType()) ? null : bo.getReceiverIds());
            result = messageDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            messageStatusService.save(bo.getId(), MessageBO.RECEIVE_TYPE_ALL.equals(bo.getReceiveType()) ? null : bo.getReceiverIds());
            result = messageDao.insert(bo);
        }

        sendMessage(bo);
        return result;
    }

    public void sendMessage(MessageBO bo) {
        if (MessageBO.TYPE_NOTICE.equals(bo.getType())) {
            String[] systemIds = bo.getSystemIds().split(",");
            for (String systemId : systemIds) {
                MessageUtil.publish("message.send." + systemId + ".all", "");
            }
        } else if (MessageBO.TYPE_ANNOUNCE.equals(bo.getType())) {
            String[] systemIds = bo.getSystemIds().split(",");
            for (String systemId : systemIds) {
                if (MessageBO.RECEIVE_TYPE_ALL.equals(bo.getReceiveType())) {
                    MessageUtil.publish("message.send." + systemId + ".all", "");
                } else {
                    String[] receiverIds = bo.getReceiverIds().split(",");
                    for (String receiverId : receiverIds) {
                        if (receiverId.contains("@")) {
                            MessageUtil.publish("message.send." + systemId + "." + receiverId.split("@")[1], "");
                        } else {
                            MessageUtil.publish("message.send." + systemId + ".", "");

                        }
                    }
                }
            }
        } else if (MessageBO.TYPE_MESSAGE.equals(bo.getType())) {
            if (MessageBO.RECEIVE_TYPE_ALL.equals(bo.getReceiveType())) {
                MessageUtil.publish("message.send.all.all", "");
            } else {
                String[] receiverIds = bo.getReceiverIds().split(",");
                for (String receiverId : receiverIds) {
                    if (receiverId.contains("@")) {
                        MessageUtil.publish("message.send.all." + receiverId.split("@")[1], "");
                    } else {
                        MessageUtil.publish("message.send.all." + receiverId, "");

                    }
                }
            }
        }
    }
    
    @WriteTransactional
    public int delete(MessageBO bo) {
        if (bo.getIds() != null) {
            for (String id : bo.getIds()) {
                messageStatusService.deleteByMessageId(id);
            }
        } else if (Tool.isNotBlank(bo.getId())) {
            messageStatusService.deleteByMessageId(bo.getId());
        }
        return messageDao.delete(bo);
    }

    public int queryUnreadCount(MessageBO bo) {
        return messageDao.queryUnreadCount(bo);
    }

    public PageInfo<MessageBO> queryMessagePage(MessageBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(messageDao.queryMessageList(bo));
    }
}
